<?php

namespace app\admin\controller;

use app\admin\validate\StoreOrderValidate;
use app\service\traits\Crud;
use app\admin\model\StoreOrder as StoreOrderModel;
use think\facade\Request;
use think\Response;

class StoreOrder extends Base
{
    private StoreOrderModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new StoreOrderModel();
        $this->validateClass = StoreOrderValidate::class;
        $this->field         = 'id,no,create_time,order_status,settlement_status,channel,pay_type,company_id';
        $this->append        = "store_order_status_text,channel_text,pay_type_text,settlement_status_text";
        $this->isGs          = true;
        $this->withSearch    = 'id,no,create_time,order_status,settlement_status,company_id';
    }


    /**
     * 分账信息
     * @return Response
     */
    public function profit(): Response
    {
        $param = request()->param();
        validate($this->validateClass)->scene('list')->check($param);
        $sql   = $this->model->orderStatus(4);
        $sql   = $sql->withSearch($this->withSearch, $param)->field($this->field);
        $count = $sql->count();
        if (Request::param('is_export', 0) != 1) $sql = $sql->page($param['page'], $param['limit']);
        $list = $sql->order($this->orderSort)->select()->each(function ($item) {
            //订单总金额
            $item->total_money = $item->storeOrderDetails()->sum('amount');
            //实收金额
            $item->real_money = $item->total_money;
            if ($item->pay_type == 2) {
                $item->real_money = bcmul($item->total_money, config('platform.wechat_profit'), 2);
            }
            $item->company_name = $item->company()->value('name');
            //分佣信息
            //合伙人分佣
            $item->company_profit = $item->storeOrderProfits()->sum('company_profit');
            //平台分佣
            $item->platform_profit = $item->storeOrderProfits()->sum('platform_profit');
            //司机分佣
            $item->driver_profit = $item->storeOrderProfits()->sum('driver_profit');
        })->append(explode(',', $this->append));

        //已结算金额 减去-微信支付的费率
        $settled_arr      = $this->model->where('settlement_status', 2)
                                        ->withSum('storeOrderDetails', 'amount')
                                        ->orderStatus(4)
                                        ->withSearch($this->withSearch, $param)
                                        ->select()->column('store_order_details_sum');
        $settled_wx_arr   = $this->model->where('settlement_status', 2)
                                        ->withSum('storeOrderDetails', 'amount')
                                        ->orderStatus(4)->where('pay_type', 2)
                                        ->withSearch($this->withSearch, $param)
                                        ->select()->column('store_order_details_sum');
        $settled_wx_money = bcadd(array_sum($settled_wx_arr), 0, 2);
        $settled_wx_money = bcmul($settled_wx_money, (config('platform.wechat_profit') - 1), 2);
        $settled_money    = bcadd(array_sum($settled_arr), $settled_wx_money, 2);

        //未结算金额 减去-微信支付的费率
        $unsettled_arr      = $this->model->where('settlement_status', 1)
                                          ->withSum('storeOrderDetails', 'amount')
                                          ->orderStatus(4)
                                          ->withSearch($this->withSearch, $param)
                                          ->select()->column('store_order_details_sum');
        $unsettled_wx_arr   = $this->model->where('settlement_status', 1)
                                          ->withSum('storeOrderDetails', 'amount')
                                          ->orderStatus(4)->where('pay_type', 2)
                                          ->withSearch($this->withSearch, $param)
                                          ->select()->column('store_order_details_sum');
        $unsettled_wx_money = bcadd(array_sum($unsettled_wx_arr), 0, 2);
        $unsettled_wx_money = bcmul($unsettled_wx_money, (config('platform.wechat_profit') - 1), 2);
        $unsettled_money    = bcadd(array_sum($unsettled_arr), $unsettled_wx_money, 2);

        return $this->success('获取成功', compact('count', 'list', 'settled_money', 'unsettled_money'));
    }

    /**
     * 批量结算
     * @param $order_ids
     * @return Response
     */
    public function edit($order_ids): Response
    {
        $this->model->whereIn("id", $order_ids)->update(['settlement_status' => 2]);
        //后期可以有生产结算单操作,账户钱直接分到结算账户
        return $this->success('操作成功');
    }
}